﻿using System.Collections.Generic;
using AdAggregator.Util;
using AdAggregator.Website;
using ClothingAggregator.Models.Parser;
using ClothingAggregator.Models.SearchData;
using ClothingAggregator.Utils;
using SortingDirection = AdAggregator.SearchData.SortingDirection;

namespace ClothingAggregator.Models.Website
{
    public class BrandPlWebsite : BaseWebsite<MySearchData, BrandPlSearchData, MyAdvertisement, BrandPlParser>
    {
        public BrandPlWebsite(BrandPlParser parser)
            : base(parser)
        {
        }

        protected override BrandPlSearchData MapSearchData(MySearchData searchData)
        {
            var data = new BrandPlSearchData();
            if (HasUnsupportedField(searchData))
            {
                data.IsSearchUnsupported = true;
                return data;
            }
            data.Sex = MapSex(searchData.Sex, searchData.Age);
            data.Age = MapAge(searchData.Age, searchData.Sex);
            data.Colour = MapColor(searchData.Color);
            data.Category = MapCategory(searchData.Category);
            data.OrderBy = MapOrderBy(searchData.OrderBy, searchData.SortingDirection);
            data.Size = SizeMap[searchData.Size];
            data.Page = searchData.Page;
            if (searchData.PriceMin > 0) data.PriceMin = searchData.PriceMin.ToString();
            if (searchData.PriceMax > 0) data.PriceMax = searchData.PriceMax.ToString();
            return data;
        }

        private string MapOrderBy(string orderBy, SortingDirection sortingDirection)
        {
            if (orderBy == "Price" && sortingDirection == SortingDirection.ASC) return "price";
            if (orderBy == "Price" && sortingDirection == SortingDirection.DESC) return "price_desc";
            return "date";
        }

        private string MapCategory(ClothingCategory category)
        {
            if (category == ClothingCategory.Underwear || category == ClothingCategory.Nightwear) return "akcesoria&subcategory=bielizna";
            if (category == ClothingCategory.Blouse) return "t-shirty|koszule|bluzy";
            if (category == ClothingCategory.Sweatshirt) return "bluzy";
            if (category == ClothingCategory.Vest || category == ClothingCategory.Jacket || category == ClothingCategory.Coat) return "kurtki";
            if (category == ClothingCategory.Shirt) return "koszule";
            if (category == ClothingCategory.Top || category == ClothingCategory.Tshirt) return "t-shirty";
            if (category == ClothingCategory.Polo) return "t-shirty&teetype=polo";
            if (category == ClothingCategory.Shorts) return "spodnie&type=krotkie";
            if (category == ClothingCategory.Skirt) return "spodnice";
            if (category == ClothingCategory.Dress) return "sukienki";
            if (category == ClothingCategory.Sweater) return "swetry";
            if (category == ClothingCategory.Swimsuit) return "akcesoria&subcategory=bielizna&underwear=kapielowki";
            return category.GetEnumName().ToLatinString().ToLowerInvariant();
        }

        private string MapColor(ClothingColor colour)
        {
            if (colour == ClothingColor.Any) return string.Empty;
            if (colour == ClothingColor.Rainbow) return "multikolor";
            if (colour == ClothingColor.Navy) return "ciemnoniebieski";
            if (colour == ClothingColor.Blue) return "jasnoniebieski|niebieski";
            return colour.GetEnumName().ToLatinString().ToLowerInvariant();
        }

        private string MapAge(ClothingAge age, ClothingSex sex)
        {
            if (age == ClothingAge.Child && sex == ClothingSex.Men) return "chlopiec";
            if (age == ClothingAge.Child && sex == ClothingSex.Women) return "dziewczynka";
            return string.Empty;
        }

        private string MapSex(ClothingSex sex, ClothingAge age)
        {
            if (age == ClothingAge.Child) return "dzieciece";
            if (sex == ClothingSex.Men) return "meskie";
            if (sex == ClothingSex.Women) return "damskie";
            return string.Empty;
        }

        private bool HasUnsupportedField(MySearchData searchData)
        {
            if (searchData.Category == ClothingCategory.Blazer ||
                searchData.Category == ClothingCategory.Suit ||
                searchData.Size == ClothingSize.XXS ||
                 searchData.Size == ClothingSize.Size37 ||
                 searchData.Size == ClothingSize.Size39 ||
                 searchData.Size == ClothingSize.Size41 ||
                 searchData.Size == ClothingSize.Size43 ||
                 searchData.Size == ClothingSize.Size45 ||
                 searchData.Size == ClothingSize.Size47 ||
                 searchData.Size == ClothingSize.Size48 ||
                searchData.Size == ClothingSize.OneSize)
                return true;
            return false;
        }

        private static readonly Dictionary<ClothingSize, string> SizeMap = new Dictionary<ClothingSize, string>
            {
                {ClothingSize.Any, ""},
                {ClothingSize.XXL, "7"},
                {ClothingSize.XL, "6"},
                {ClothingSize.L, "5"},
                {ClothingSize.M, "4"},
                {ClothingSize.S, "3"},
                {ClothingSize.XS, "2"},
                {ClothingSize.Size24, "310|9|282|95|384|392|400"},
                {ClothingSize.Size25, "283|10|179|96|346|390|292"},
                {ClothingSize.Size26, "11|12|13|14|97|293|347|391|"},
                {ClothingSize.Size27, "15|16|17|18|98|316|348|393|294"},
                {ClothingSize.Size28, "19|20|21|22|99|318|287|295|349|394|319"},
                {ClothingSize.Size29, "23|24|25|26|100|288|296|350|395|321|401"},
                {ClothingSize.Size30, "27|28|29|30|101|297|351|396|323|402"},
                {ClothingSize.Size31, "31|32|33|34|102|385|398|325|235|380"},
                {ClothingSize.Size32, "35|36|37|38|103|386|397|341|381"},
                {ClothingSize.Size33, "39|40|41|42|104|389|333|342|382|301"},
                {ClothingSize.Size34, "43|44|45|46|105|399|383|171"},
                {ClothingSize.Size35, "302|303|284|304|328"},
                {ClothingSize.Size36, "47|48|403|49|50|172|106"},
                {ClothingSize.Size38, "51|52|53|54|173|107"},
                {ClothingSize.Size40, "174|55|56|175|177"},
                {ClothingSize.Size42, "305|57|58"},
                {ClothingSize.Size44, "59|60|299"},
                {ClothingSize.Size46, "309|306"},
                {ClothingSize.SizeChildTo1Y, "163|164|165"},
                {ClothingSize.SizeChild1YTo5Y, "166|167|169|278|139|140|141"},
                {ClothingSize.SizeChild5YTo10Y, "142|143|361|144|362"},
                {ClothingSize.SizeChild10YTo16Y, "145|358|146|359|147|311"}
            };
    }
}